#include "Texture.h"


Texture::Texture(LPDIRECT3DDEVICE9 d3ddv, LPWSTR file_name, int cols, int rows, int count, D3DCOLOR trans_color)
{
	this->d3ddv = d3ddv;
	this->file_name = file_name;
	this->trans_color = trans_color;
	this->texture = NULL;

	this->cols = cols;
	this->rows = rows;
	this->count = count;

	this->Load();
}

Texture::~Texture()
{
	if (texture)
	{
		texture->Release();
	}
}

void Texture::Load()
{
	D3DXIMAGE_INFO info; 
	HRESULT result;

	//Lay thong tin cua file anh
	result = D3DXGetImageInfoFromFile(file_name, &info);

	this->width = info.Width / this->cols;
	this->height = info.Height / this->rows;

	if (result != D3D_OK)
	{
		trace(L"[ERROR] Failed to get information from image file '%s'", file_name);
		return;
	}

	result = D3DXCreateTextureFromFileEx(
		this->d3ddv,
		this->file_name,
		info.Width,
		info.Height,
		1,
		D3DUSAGE_DYNAMIC,
		D3DFMT_UNKNOWN,
		D3DPOOL_DEFAULT,
		D3DX_DEFAULT,
		D3DX_DEFAULT,
		trans_color,//D3DCOLOR_XRGB(255, 255, 255),
		&info,
		NULL,
		&texture
		);

	if (result != D3D_OK) 
	{
		trace(L"[ERROR] Failed to create texture from file '%s'", file_name);
		return;
	}
}